#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package ${package}.frontapi.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import lombok.extern.slf4j.Slf4j;
import org.modelmapper.ModelMapper;
import org.modelmapper.PropertyMap;
import org.modelmapper.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import uyun.bird.tenant.api.UserService;
import uyun.bird.tenant.api.entity.Pagination;
import uyun.bird.tenant.api.entity.User;
import uyun.bird.tenant.api.entity.UserQueryParam;
import ${package}.common.ApiSupport;
import ${package}.common.RequestParamsHolder;
import ${package}.config.AppProperties;
import ${package}.frontapi.UserFrontApi;
import ${package}.frontapi.vo.UsersVO;
import uyun.tenant.api.entify.PrivilegeRequest;

import java.util.ArrayList;
import java.util.List;

import static ${package}.common.ApiSupport.KEY_TENANT_ID;


/**
 * @author: yangfei
 * @Date: 2019/4/18 Time: 15:52
 * @desc 获取用户信息
 */

@Slf4j
@RestController
public class UsersFrontImpl implements UserFrontApi {

    @Autowired
    private AppProperties appProperties;

    @Reference
    private UserService userService;


    @Override
    public List<UsersVO> queryAppUsers(String realName) {

        String tenantId = RequestParamsHolder.getParameter(KEY_TENANT_ID);
        UserQueryParam userQueryParam = new UserQueryParam();
        userQueryParam.setProductNum(appProperties.getCode());
        userQueryParam.setTenantId(tenantId);
        userQueryParam.setRealName(realName);
        Pagination<User> userPagination = userService.listBy(userQueryParam);

        List<UsersVO> usersVOList = new ArrayList<>();
        ModelMapper modelMapper = modelMapperUsersVO();
        usersVOList = modelMapper.map(userPagination.getRecords(), new TypeToken<List<PrivilegeRequest>>() {
        }.getType());
        return usersVOList;
    }


    private ModelMapper modelMapperUsersVO() {
        ModelMapper modelMapper = new ModelMapper();
        //创建自定义映射规则
        PropertyMap<User, UsersVO> propertyMap = new PropertyMap<User, UsersVO>() {
            @Override
            protected void configure() {
                map(source.getInviteCode(), destination.getInviteCode());//使用自定义转换规则
                map(source.getQq(), destination.getQq());//使用自定义属性提供覆盖
                map(source.getSite(), destination.getSite());
                map(source.getSkin(), destination.getSkin());
                map(source.getStatus(), destination.getStatus());
                map(source.getUserNo(), destination.getUserNo());
                map(source.getWeixin(), destination.getWeixin());
            }
        };
        //添加映射器
        modelMapper.addMappings(propertyMap);
        modelMapper.validate();
        return modelMapper;
    }
}
